<?php
error_reporting(E_ERROR | E_PARSE);
//test_targeting script created by Robert Cox on April 1st 2011
//Commandline Usage: ./test_targeting.php commandline (debug)
//URL Usage: http://.../../test_targeting.php?web=1              <= normal mode
//      -or- http://.../../test_targeting.php?web=1&debug=1      <= debug mode

// set start time
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$command1 = '/usr/local/nagios/libexec/check_nrpe -H wdev1 -c check_cores_detailed';
$command2 = '/usr/bin/ssh -i /sharedvolume/aimatchwest.pem root@wdev1 /usr/local/nagios/libexec/check_cores_detailed_dev';

if(isset($_GET['debug'])){
                $debug = TRUE;
                $GLOBALS["debug"] = TRUE;
        } else {
                unset($debug);
        }

if(isset($_GET['web'])){
                $web = TRUE;
                $GLOBALS["web"] = TRUE;
        } else {
                unset($web);
        }


if (isset($argv[1])) { $commandline = $argv[1]; unset($debug);} 
		
if (isset($debug)) { 
	echo "<br>* * * * *  D E B U G = O N * * * * *<br>";
	if (isset($web)) { 
		echo "<br>* * * * *  W E B = O N  * * * * *<br>";}
}


$status = "";
$testname = "Core File Test";
$testdesc = "Verifies that the processes have not cored";
$testurl = "test_cores.php";
$testtime = time();

// Setup the archival file name for the test
$datestamp = date("YmdGis");
$directory = "/http/matchtest/archived_results/corestest/";
#$directory = "/tmp/canary_archived_results/targetingtest/";
if(!is_dir($directory)){
	if(!mkdir($directory, 0777)) {
		die('Failed to create directory: '.$directory);
	}
}

$today = date("F j, Y, g:i a");

exec($command1,$output1,$return_var1);

if (isset($debug)) { echo "<br><pre><hr>Command Output version 1:<br><br>"; print_r($output1); echo "</pre><br>"; }
if (isset($debug)) { echo "Return Var 1: <br>".$return_var1."<br><hr>"; }

$numcores = count($output1);

// Determine if the test was successful
if($return_var1){ 
	$message1 = "***** ALERT - ".$output1[0]." found in the /cores directory on WDEV1. \n";
	$message2 = "***** Core file test FAILED. \n";
	$message3 = "***** Writing out failure to testresults.txt\n";
	if (isset($web)) { echo $message1."<br>";}
    if (isset($web)) { echo $message2."<br>";}
	if (isset($web)) { echo $message3."<br><hr>"; }
    if (isset($commandline)) { echo $message1."\n\n";}
    if (isset($commandline)) { echo $message2."\n\n";}
	if (isset($commandline)) { echo $message3."\n\n"; }
	$status = "fail";			
	
}
else { 
	$message1 = "***** SUCCESS - There are no core files in /cores on WDEV1";
	$message2 = "***** Core file test PASSED";
	$message3 = "***** Writing out Pass to testresults.txt";
	if (isset($web)) { echo $message1."<br>";}
    if (isset($web)) { echo $message2."<br>";}
    if (isset($web)) { echo $message3."<br>";}
	if (isset($commandline)) { echo $message1."\n\n";}
    if (isset($commandline)) { echo $message2."\n\n";}
    if (isset($commandline)) { echo $message3."\n\n";}
	$status = "pass";
}

// Write out the information to the testresults.txt file.

$lines = file('/http/matchtest/testresults.txt');
$numitems = count($lines);

// Loop through our array, show HTML source as HTML source; and line numbers too.

if (isset($debug)) { echo "<br><pre><hr>HTTP Response Output:<br><br>"; print_r($lines); echo "</pre><br><hr>"; }

$newfile = $lines[0];
$i=1;
while($i <= $numitems) {
	$pieces = explode("|",$lines[$i]);
	if($testname == $pieces[1]){
		$replaced_line = $status."|".$testname."|".$testdesc."|".$testurl."|".$testtime."\n";
		$newfile = $newfile.$replaced_line;
	}
	else{
		$newfile = $newfile.$lines[$i];
	}
	$i++;
}

if (isset($debug)) { echo "<br><pre><hr>New File Output:<br><br>"; print_r($newfile); echo "</pre><br><hr>"; }

$fp = fopen('/http/matchtest/testresults.txt', 'w');
fwrite($fp, $newfile);
fclose($fp);

if (isset($debug)) { phpinfo(); }
?>
